import androidx.compose.animation.core.*
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

fun main() = application {
    Window(
        onCloseRequest = ::exitApplication,
        title = "KMP-TimerClock"
    ) {
        Clock()
    }
}

@Composable
@Preview
fun BackGround() {
    Canvas(modifier = Modifier.fillMaxSize()) {
        drawCircle(
            brush = Brush.sweepGradient(
                colors = listOf(Color(0XFFF18998), Color(0XFFF5BA96), Color(0XFFF8EB94)),
            ),
            radius = 120f,
        )
    }
}

@Composable
@Preview
fun Clock() {
    // 绘制表盘
    Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth()) {
        val hourState = remember { Animatable(0f) }
        val minState = remember { Animatable(0f) }
        val secondState = remember { Animatable(0f) }
        val date = Date()
        val format = SimpleDateFormat("yyyy-MM-dd")
        val dayTime = format.parse(format.format(date)).time
        val num = date.time - dayTime
        LaunchedEffect(Unit) {
            val hour = (num / 1000f / 60f / 60f) % 12f
            println("hour: $hour")
            hourState.snapTo(hour % 12 * 30f)
            hourState.animateTo(
                targetValue = hour % 12 * 30f + 360f,
                animationSpec = infiniteRepeatable(animation = tween(1000 * 3600 * 12, easing = LinearEasing))
            )
        }
        LaunchedEffect(Unit) {
            val minute = (num / 1000f / 60f) % 60f
            println("minute $minute")
            minState.snapTo(minute * 6f)
            minState.animateTo(
                targetValue = minute * 6f + 360f,
                animationSpec = infiniteRepeatable(animation = tween(3600 * 1000, easing = LinearEasing))
            )
        }
        LaunchedEffect(Unit) {
            val second = (num / 1000f) % 60f
            println("second $second")
            secondState.snapTo(second * 6f)
            secondState.animateTo(
                targetValue = second * 6f + 360f,
                animationSpec = infiniteRepeatable(animation = tween(60000, easing = LinearEasing))
            )
        }
        Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            // 渐变色的练习
            BackGround()
            Image(
                painterResource("img.png"),
                contentDescription = "Done",
                Modifier.clip(shape = CircleShape).size(50.dp)
            )
            for (i in 0 until 60) {
                Canvas(modifier = Modifier.size(200.dp).rotate(i * 6f)) {
                    drawLine(
                        start = Offset(center.x, center.y - if (i % 5 == 0) 95f else 105f),
                        end = Offset(center.x, 0f),
                        color = if (i % 5 == 0) Color.Black else Color.DarkGray,
                        strokeWidth = if (i % 5 == 0) 2f else 1f
                    )
                }
            }
            val radius = 85f
            val avgAngle = (2 * PI) / 12
            for (index in 1..12) {
                val angle = avgAngle * index // 计算当前文本的角度
                val x = radius * cos(angle) // X 坐标
                val y = radius * sin(angle) // Y 坐标
                // 绘制文本
                Text(
                    text = (if (index > 9) index - 9 else index + 3).toString(),
                    color = Color.Black,
                    style = TextStyle(fontSize = 16.sp, fontWeight = FontWeight.Normal),
                    modifier = Modifier.offset {
                        IntOffset(x.toInt(), y.toInt())
                    }
                )
            }
            Canvas(modifier = Modifier.size(200.dp)) {
                drawCircle(
                    color = Color.DarkGray.copy(alpha = 0.5f),
                    radius = 122f,
                    center = Offset(center.x, center.y),
                    style = Stroke(10f)
                )
                drawCircle(
                    color = Color.Black.copy(alpha = 0.8f),
                    radius = 122f,
                    center = Offset(center.x, center.y),
                    style = Stroke(8f)
                )
                drawCircle(
                    color = Color.White,
                    radius = 122f,
                    center = Offset(center.x, center.y),
                    style = Stroke(7f)
                )
            }
            point(minState, color = Color(0XFF009900), length = 80f, width = 2f)
            point(hourState, color = Color(0XFF243483), length = 50f, width = 6f)
            point(secondState, color = Color.Red, length = 110f, width = 1f)
            Canvas(modifier = Modifier.size(200.dp)) {
                drawCircle(
                    color = Color.Red,
                    radius = 4f,
                    center = Offset(center.x, center.y),
                )
                drawCircle(
                    color = Color.White,
                    radius = 1f,
                    center = Offset(center.x, center.y)
                )
            }
        }
    }
}

@Preview
@Composable
fun point(degree: Animatable<Float, AnimationVector1D>, color: Color, length: Float, width: Float) {
    Canvas(modifier = Modifier.size(200.dp).rotate(degree.value)) {
        drawLine(
            start = center,
            end = Offset(center.x, center.y - length - 2f),
            color = Color.Gray.copy(alpha = 0.25f),
            strokeWidth = width + 2f
        )
        drawLine(
            start = center,
            end = Offset(center.x, center.y - length - 1f),
            color = Color.Gray.copy(alpha = 0.5f),
            strokeWidth = width + 1f
        )
        drawLine(
            start = center,
            end = Offset(center.x, center.y - length),
            color = color,
            strokeWidth = width
        )
    }
}
